#define _CRT_SECURE_NO_WARNINGS
#include "jsonOP.h"
#include"common.h"

//对json操作函数


//添加报名选手
void addApplicant(char* name, char* project, char *items) {

	FILE* file = fopen("ITEMS.json", "r");
	if (file == NULL) {
		printf("Failed to open file.\n");
	
	}

	char* content = NULL;
	size_t fileLen = 0;
	fseek(file, 0, SEEK_END);
	fileLen = ftell(file);
	fseek(file, 0, SEEK_SET);
	content = (char*)malloc(fileLen + 1);
	fread(content, 1, fileLen, file);
	fclose(file);
	content[fileLen] = '\0';

	cJSON* root = cJSON_Parse(content);
	cJSON* thisProject = cJSON_GetObjectItem(root, project);
	cJSON* thisItems = cJSON_GetObjectItem(thisProject, items);
	cJSON* thisONE = cJSON_GetObjectItem(thisItems, "applicants");
	cJSON_AddItemToArray(thisONE, cJSON_CreateString(name));
	char* p = cJSON_Print(root);
	printf("%s", p);
	FILE* fp = fopen("ITEMS.json", "w");
	fprintf(fp,"%s", p);
	
	free(content);
	fclose(fp);
	getchar();


}

//添加获胜选手
void addWinner(char* name, char* project, char* items) {
	FILE* file = fopen("ITEMS.json", "r");
	if (file == NULL) {
		printf("Failed to open file.\n");

	}

	char* content = NULL;
	size_t fileLen = 0;
	fseek(file, 0, SEEK_END);
	fileLen = ftell(file);
	fseek(file, 0, SEEK_SET);
	content = (char*)malloc(fileLen + 1);
	fread(content, 1, fileLen, file);
	fclose(file);
	content[fileLen] = '\0';

	cJSON* root = cJSON_Parse(content);
	cJSON* thisProject = cJSON_GetObjectItem(root, project);
	cJSON* thisItems = cJSON_GetObjectItem(thisProject, items);
	cJSON* thisONE = cJSON_GetObjectItem(thisItems, "winners");
	cJSON_AddItemToArray(thisONE, cJSON_CreateString(name));

	if (root == NULL) {
		printf("Error before: [%s]\n", cJSON_GetErrorPtr());
		free(content);

	}
	char* p = cJSON_Print(root);
	printf("%s", p);
	FILE *fp=fopen("ITEMS.json", "w");
	fprintf(fp, "%s", p);
	fclose(fp);
	getchar();
}

//添加大类 
void addProject(char* project) {
	FILE* file = fopen("ITEMS.json", "r");
	if (file == NULL) {
		printf("Failed to open file.\n");

	}

	char* content = NULL;
	size_t fileLen = 0;
	fseek(file, 0, SEEK_END);
	fileLen = ftell(file);
	fseek(file, 0, SEEK_SET);
	content = (char*)malloc(fileLen + 1);
	fread(content, 1, fileLen, file);
	fclose(file);
	content[fileLen] = '\0';

	cJSON* root = cJSON_Parse(content);
	if (root == NULL) {
		printf("Error before: [%s]\n", cJSON_GetErrorPtr());
		free(content);

	}

	cJSON* newProject = cJSON_CreateObject();
	cJSON_AddItemToObject(root, project, newProject);
	
	char* p = cJSON_Print(root);
	printf("%s", p);
	FILE* f = fopen("ITEMS.json", "w");
	if (f == NULL) {
		printf("err");
	}
	//fwrite(NULL, 1, 0, f);
 fprintf(f, "%s", p);
	
	fclose(f);
	free(content);

	getchar();
}


//添加小类 
void addItems(char* project, char* items) {
	FILE* file = fopen("ITEMS.json", "r");
	if (file == NULL) {
		printf("Failed to open file.\n");

	}

	char* content = NULL;
	size_t fileLen = 0;
	fseek(file, 0, SEEK_END);
	fileLen = ftell(file);
	fseek(file, 0, SEEK_SET);
	content = (char*)malloc(fileLen + 1);
	fread(content, 1, fileLen, file);
	fclose(file);
	content[fileLen] = '\0';

	cJSON* root = cJSON_Parse(content);
	if (root == NULL) {
		printf("Error before: [%s]\n", cJSON_GetErrorPtr());
		free(content);

	}
	cJSON* project1 = cJSON_GetObjectItem(root, project);
	cJSON* newitems = cJSON_CreateObject();

	cJSON_AddItemToObject(project1, items, newitems);
	cJSON_AddArrayToObject(newitems, "applicants");
	cJSON_AddArrayToObject(newitems, "winners");


	char* p = cJSON_Print(root);
	printf("%s", p);
	FILE* f = fopen("ITEMS.json", "w");
	if (f == NULL) {
		printf("err");
	}

	long a = fprintf(f, "%s", p);
	printf("%ld", a);
	fclose(f);
	free(content);

	getchar();
}


//删除大类 
void deleteProject(char* project){
	FILE* file = fopen("ITEMS.json", "r");
	if (file == NULL) {
		printf("Failed to open file.\n");

	}

	char* content = NULL;
	size_t fileLen = 0;
	fseek(file, 0, SEEK_END);
	fileLen = ftell(file);
	fseek(file, 0, SEEK_SET);
	content = (char*)malloc(fileLen + 1);
	fread(content, 1, fileLen, file);
	fclose(file);
	content[fileLen] = '\0';

	cJSON* root = cJSON_Parse(content);
	if (root == NULL) {
		printf("Error before: [%s]\n", cJSON_GetErrorPtr());
		free(content);

	}
	
	cJSON_DeleteItemFromObject(root,project);


	char* p = cJSON_Print(root);
	printf("%s", p);
	FILE* f = fopen("ITEMS.json", "w");
	if (f == NULL) {
		printf("err");
	}

	long a = fprintf(f, "%s", p);
	printf("%ld", a);
	fclose(f);
	free(content);

	getchar();
}

//删除小项 
void deleteItems(char* project, char* items) {
	FILE* file = fopen("ITEMS.json", "r");
	if (file == NULL) {
		printf("Failed to open file.\n");

	}

	char* content = NULL;
	size_t fileLen = 0;
	fseek(file, 0, SEEK_END);
	fileLen = ftell(file);
	fseek(file, 0, SEEK_SET);
	content = (char*)malloc(fileLen + 1);
	fread(content, 1, fileLen, file);
	fclose(file);
	content[fileLen] = '\0';

	cJSON* root = cJSON_Parse(content);
	if (root == NULL) {
		printf("Error before: [%s]\n", cJSON_GetErrorPtr());
		free(content);

	}
	cJSON* target = cJSON_GetObjectItem(root, project);
	cJSON_DeleteItemFromObject(target, items);
	

	char* p = cJSON_Print(root);
	printf("%s", p);
	FILE* f = fopen("ITEMS.json", "w");
	if (f == NULL) {
		printf("err");
	}

	long a=fprintf(f,"%s",p);
	printf("%ld", a);
	fclose(f);
	free(content);

	getchar();
}


//查看小类名单 
void serchItems(char* project, char* items) {
	FILE* file = fopen("ITEMS.json", "r");
	if (file == NULL) {
		printf("Failed to open file.\n");

	}

	char* content = NULL;
	size_t fileLen = 0;
	fseek(file, 0, SEEK_END);
	fileLen = ftell(file);
	fseek(file, 0, SEEK_SET);
	content = (char*)malloc(fileLen + 1);
	fread(content, 1, fileLen, file);
	fclose(file);
	content[fileLen] = '\0';

	cJSON* root = cJSON_Parse(content);
	if (root == NULL) {
		printf("Error before: [%s]\n", cJSON_GetErrorPtr());
		free(content);

	}
	cJSON* target = cJSON_GetObjectItem(root, project);
	cJSON* targetFinal = cJSON_GetObjectItem(target, items);
	char* p = cJSON_Print(targetFinal);
	printf("%s", p);
	free(content);
	
	getchar();

}

//查看大类名单 
void serchProject(char* project)
{

	FILE* file = fopen("ITEMS.json", "r");
	if (file == NULL) {
		printf("Failed to open file.\n");

	}

	char* content = NULL;
	size_t fileLen = 0;
	fseek(file, 0, SEEK_END);
	fileLen = ftell(file);
	fseek(file, 0, SEEK_SET);
	content = (char*)malloc(fileLen + 1);
	fread(content, 1, fileLen, file);
	fclose(file);
	content[fileLen] = '\0';

	cJSON* root = cJSON_Parse(content);
	if (root == NULL) {
		printf("Error before: [%s]\n", cJSON_GetErrorPtr());
		free(content);

	}
	cJSON* target = cJSON_GetObjectItem(root, project);
	char* p = cJSON_Print(target);
	printf("%s", p);
	free(content);
	getchar();

}

//查看所有项目 
void serchAll() {

	FILE* file = fopen("ITEMS.json", "r");
	if (file == NULL) {
		printf("Failed to open file.\n");
		
	}

	char* content = NULL;
	size_t fileLen = 0;
	fseek(file, 0, SEEK_END);
	fileLen = ftell(file);
	fseek(file, 0, SEEK_SET);
	content = (char*)malloc(fileLen + 1);
	fread(content, 1, fileLen, file);
	fclose(file);
	content[fileLen] = '\0';

	cJSON* root = cJSON_Parse(content);

	if (root == NULL) {
		printf("Error before: [%s]\n", cJSON_GetErrorPtr());
		free(content);
		
	}
	char* p = cJSON_Print(root);
	printf("%s", p);
	free(content);
	getchar();

}

//删除获奖人员
void deletArr(char* project, char* item) {
	FILE* file = fopen("ITEMS.json", "r");
	if (file == NULL) {
		printf("Failed to open file.\n");

	}
	char* content = NULL;
	size_t fileLen = 0;
	fseek(file, 0, SEEK_END);
	fileLen = ftell(file);
	fseek(file, 0, SEEK_SET);
	content = (char*)malloc(fileLen + 1);
	fread(content, 1, fileLen, file);
	fclose(file);
	content[fileLen] = '\0';

	cJSON* root = cJSON_Parse(content);
	cJSON* P = cJSON_GetObjectItem(root, project);
	cJSON* PP = cJSON_GetObjectItem(P, item);
	cJSON* PPP = cJSON_GetObjectItem(PP, "winners");
	int size = cJSON_GetArraySize(PPP);
	cJSON_DeleteItemFromArray(PPP, size - 1);
	FILE* f = fopen("ITEMS.json", "w");
	if (f == NULL) {
		printf("Failed to open file.\n");

	}
	char* p = cJSON_Print(root);
	fprintf(f, "%s", p);
	printf("%s", p);
	free(content);
	getchar();

}